package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.GoodImageEntity;
import cn.com.coding.system.domin.ProductImageEntity;
import cn.com.coding.system.domin.UserCollectEntity;
import cn.com.coding.system.domin.dto.UserCollectDto;
import cn.com.coding.system.service.ProductImageService;
import cn.com.coding.system.service.UserCollectService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 收藏
 *
 * @author mxz
 * @email codingce@gmail.com
 * @date 2022-01-08 21:25:22
 */
@RestController
@RequestMapping("cat/userCollect")
public class UserCollectController {

    @Resource
    private UserCollectService userCollectService;

    @Resource
    private ProductImageService productImageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResT list(@RequestParam Map<String, Object> params) {
        PageUtil page = userCollectService.queryPage(params);

        return ResT.ok().put("page", page);
    }

    /**
     * 用户获取全部收藏
     */
    @RequestMapping("/getCollect")
    public ResT getCollect(@RequestParam("userPid") String userPid) {

        Set<UserCollectDto> collectList = userCollectService.getUserCollectByUserPid(userPid);
        collectList.forEach(e -> {
            List<String> list = productImageService.list(new QueryWrapper<ProductImageEntity>().eq("product_pid", e.getProductPid())).stream().map(ProductImageEntity::getImgUrl).collect(Collectors.toList());
            e.setImgUrl(list);
        });

        return ResT.ok().setData(MapUtil.builder().put("collectList", collectList).build());
    }

    /**
     * 用户添加收藏
     */
    @PostMapping("/addCollect")
    public ResT addCollect(@RequestBody Map<String, Object> params) {

        String productPid = (String) params.get("productPid");
        String userPid = (String) params.get("userPid");

        UserCollectEntity oldCollect = userCollectService.getOne(new QueryWrapper<UserCollectEntity>().eq("user_pid", userPid).eq("product_pid", productPid));

        if (oldCollect != null) return ResT.ok(208, "该景点已经添加收藏，请到我的收藏查看");

        UserCollectEntity userCollectEntity = new UserCollectEntity();
        userCollectEntity.setUserPid(userPid);
        userCollectEntity.setProductPid(productPid);
        userCollectService.save(userCollectEntity);
        return ResT.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        UserCollectEntity userCollect = userCollectService.getById(pid);

        return ResT.ok().put("userComment", userCollect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody UserCollectEntity userCollect) {
        userCollectService.save(userCollect);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody UserCollectEntity userCollect) {
        userCollectService.updateById(userCollect);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        userCollectService.removeByIds(Arrays.asList(pids));

        return ResT.ok();
    }

    /**
     * 用户删除收藏景点
     */
    @RequestMapping("/deleteCollect")
    public ResT deleteCollect(@RequestParam("productPid") String productPid) {

        userCollectService.remove(new QueryWrapper<UserCollectEntity>().eq("product_pid", productPid));

        return ResT.ok();
    }

}
